package com.sevlet.setting;

import com.Bean.RoomRate;
import com.Bean.RoomType;
import com.dao.ConsumerDao;
import com.dao.RoomRateDao;
import com.dao.RoomTypeDao;
import com.dao.impl.RoomRateDaoImpl;
import com.dao.impl.RoomTypeDaoImpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sevlet.BaseSevlet;
import com.util.JsonUtil;
import com.util.PageUtil;
import com.util.ProxyUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/RoomRateServlet")
public class RoomRateServlet extends BaseSevlet {
    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //StatisticsThreeDao statisticsThreeDao = new StatisticsDaoThreelmpl();
        RoomRateDao roomRateDao = new RoomRateDaoImpl();
        //RoomListThree roomListThree = new RoomListThree();
        RoomRate roomRate = new RoomRate();
        resp.setContentType("text/html;charset=utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;


        //System.out.println(req.getParameter("bookInfo"));

        try {
            out = resp.getWriter();


        } catch (IOException e) {
            e.printStackTrace();
        }
        String curentPage =   req.getParameter("currentPage");
        try {
            PageUtil<RoomRate> roomTypePageUtil =  roomRateDao.queryDataBypage(Integer.parseInt(curentPage),15,roomRate);

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
            System.out.println(roomTypePageUtil);
            jsonUtil.setData(roomTypePageUtil);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }  finally {
            out.print(gson.toJson(jsonUtil));
        }
    }

    public void updateDate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RoomRateDao roomRateDao = new RoomRateDaoImpl();
        RoomRateDao daoProxy = (RoomRateDao) ProxyUtil.getDaoProxy(roomRateDao);
        //RoomListThree roomListThree = new RoomListThree();


        JsonUtil jsonUtil = new JsonUtil();
        try {
            RoomRate roomRate = new RoomRate();

            System.out.println(req.getParameter("eid"));

            //将客户端传递过来的请求参数封装到bookBean对象中
            //要求：参数名必需与bookBean中的属性名有对应关系
            System.out.println("反射前的BookBean对象中的属性值为:"+roomRate);
            BeanUtils.populate(roomRate,req.getParameterMap());
            System.out.println("反射后的BookBean对象中的属性值为:"+roomRate);

            int res =  daoProxy.update(roomRate);

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");

        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();

        String jsonStr =  gson.toJson(jsonUtil);


        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);
    }
}
